//
//  NRWFilterVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/11.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBuyCarFilterVC.h"

#import "NRWBuyCarFilterHeader.h"
#import "NRWSliderCollectionCell.h"
#import "NRWBuyCarFilterManager.h"
#import "NRWGetFilterInfoRequest.h"
#import "NRWBuyCarFilterOptionsModel.h"
#import "NRWFilterColorCell.h"
#import "NRWPriceChooseCell.h"
#import "NRWBuyCarFilterHeaderModel.h"
#import "NRWBuyCarConditionModel.h"

@interface NRWBuyCarFilterVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout, NRWSliderCollectionCellDelegate>

@property (strong, nonatomic) NRWGetFilterInfoRequest *filterRequest;

@property (strong, nonatomic) NRWBuyCarFilterOptionsModel *model;

@property (strong, nonatomic) NSArray<NRWBuyCarFilterHeaderModel *> *sectionHeaderModel;

@property (strong, nonatomic) NSArray<NSString *> *carMileageTitleArray;

@property (strong, nonatomic) NSArray<NSString *> *carAgeTitleArray;

@end

@implementation NRWBuyCarFilterVC
{
    UICollectionView *_mCollectionView;
    UIButton *_mResetBtn;
    UIButton *_mCompleteBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    
    [SVProgressHUD show];
    [self.filterRequest start];
}

- (void)setupUI {
    
    self.navigationView.title = @"车辆筛选";
    
    [self setupCollectionView];
    
    [self setupBottomButton];
    
    [self formatSectionModel];
}

- (void)setupCollectionView {

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;

    _mCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - getSafeBottomMargin() - 45) collectionViewLayout:layout];
    _mCollectionView.backgroundColor = getColor(@"#F9F9F9");
    _mCollectionView.delegate = self;
    _mCollectionView.dataSource = self;
    _mCollectionView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    [_mCollectionView registerClass:NRWBuyCarFilterHeader.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_mCollectionView registerClass:NRWSliderCollectionCell.class forCellWithReuseIdentifier:@"slider"];
    [_mCollectionView registerClass:NRWFilterColorCell.class forCellWithReuseIdentifier:@"color"];
    [_mCollectionView registerClass:NRWPriceChooseCell.class forCellWithReuseIdentifier:@"other"];
    
    [self.view addSubview:_mCollectionView];
}

- (void)setupBottomButton {
    
    _mResetBtn = [[UIButton alloc] init];
    _mResetBtn.titleLabel.font = getThemeFont(17);
    _mResetBtn.frame = CGRectMake(0, _mCollectionView.viewMaxY, SCREEN_WIDTH() / 2, 45);
    _mResetBtn.backgroundColor = getColor(@"#FFC74A");
    [_mResetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [_mResetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_mResetBtn addTarget:self action:@selector(resetBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_mResetBtn];
    
    _mCompleteBtn = [[UIButton alloc] init];
    _mCompleteBtn.titleLabel.font = getThemeFont(17);
    _mCompleteBtn.frame = CGRectMake(SCREEN_WIDTH() / 2, _mCollectionView.viewMaxY, SCREEN_WIDTH() / 2, 45);
    _mCompleteBtn.backgroundColor = mainColor();
    [_mCompleteBtn setTitle:@"确认" forState:UIControlStateNormal];
    [_mCompleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_mCompleteBtn addTarget:self action:@selector(completeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_mCompleteBtn];
}

#pragma mark - formatSectionModel
- (void)formatSectionModel {
    
//    if (isNotEmptyString(self.filterModel.mileageTitle)) {
        self.sectionHeaderModel[0].detail = self.filterModel.mileageTitle;
//    }
    
//    if (isNotEmptyString(self.filterModel.carAgeTitle)) {
        self.sectionHeaderModel[1].detail = self.filterModel.carAgeTitle;
//    }
    
//    if (isNotEmptyString(self.filterModel.carOutColorName)) {
        self.sectionHeaderModel[2].detail = self.filterModel.carOutColorName;
//    }
    
//    if (isNotEmptyString(self.filterModel.carTypeName)) {
        self.sectionHeaderModel[3].detail = self.filterModel.carTypeName;
//    }
    
//    if (isNotEmptyString(self.filterModel.carEnvironName)) {
        self.sectionHeaderModel[4].detail = self.filterModel.carEnvironName;
//    }
}

#pragma mark - TouchAction
- (void)resetBtnDidClick:(UIButton *)sender {
    
    self.filterModel = [[NRWBuyCarFilterModel alloc] init];
    self.sectionHeaderModel = [NRWBuyCarFilterManager getBuyCarFilterTitleArray];

    [_mCollectionView reloadData];
}

- (void)completeBtnDidClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if ([self.delegate respondsToSelector:@selector(buyCarFilterVC:filterCompletedWithFilterModel:)]) {
        
        [self.delegate buyCarFilterVC:self filterCompletedWithFilterModel:self.filterModel];
    }
}

#pragma mark - requestDelegate
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    [SVProgressHUD dismiss];
    
    if (request == self.filterRequest) {
        
        self.model = [NRWBuyCarFilterOptionsModel mj_objectWithKeyValues:request.responseObject[@"result"]];
        
        [_mCollectionView reloadData];
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    
    
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return [NRWBuyCarFilterManager getBuyCarFilterTitleArray].count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0 || section == 1) {
        return 1;
    } else if (section == 2) {
        return self.model.baseCarOutColorOptions.count;
    } else if (section == 3) {
        return self.model.baseCarTypeOptions.count;
    } else {
        return self.model.baseCarEnvironOptions.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {// 里程
        NRWSliderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"slider" forIndexPath:indexPath];
        
        [cell setTitleArr:self.carMileageTitleArray firstSelectIndex:self.filterModel.mileageFirstIndex lastSelectIndex:self.filterModel.mileageLastIndex];
        cell.delegate = self;
        
        return cell;
    } else if (indexPath.section == 1) {// 车龄
        
        NRWSliderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"slider" forIndexPath:indexPath];
        
        [cell setTitleArr:self.carAgeTitleArray firstSelectIndex:self.filterModel.carAgeFirstIndex lastSelectIndex:self.filterModel.carAgeLastIndex];
        cell.delegate = self;
        
        return cell;
    } else if (indexPath.section == 2) {// 颜色
        
        NRWFilterColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"color" forIndexPath:indexPath];
        cell.colorName = self.model.baseCarOutColorOptions[indexPath.item].colorName;
        cell.colorCode = self.model.baseCarOutColorOptions[indexPath.item].colorCode;
        cell.isSelect = [cell.colorName isEqualToString:self.filterModel.carOutColorName];
        return cell;
    } else if (indexPath.section == 3) {// 车身类型
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        cell.priceTitle = self.model.baseCarTypeOptions[indexPath.item].carTypeName;
        cell.isSelect = [self.model.baseCarTypeOptions[indexPath.item].carTypeId isEqualToString:self.filterModel.carTypeId];
        return cell;
    } else {// 环保类型
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        cell.priceTitle = self.model.baseCarEnvironOptions[indexPath.item];
        cell.isSelect = [cell.priceTitle isEqualToString:self.filterModel.carEnvironName];
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        NRWBuyCarFilterHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.titleStr = self.sectionHeaderModel[indexPath.section].title;
        header.detailStr = isEmptyString(self.sectionHeaderModel[indexPath.section].detail)? @"不限": self.sectionHeaderModel[indexPath.section].detail;
        
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(SCREEN_WIDTH(), 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 || indexPath.section == 1) {
        return CGSizeMake(SCREEN_WIDTH(), 90);
    } else if (indexPath.section == 2) {
        return CGSizeMake(40 , 55);
    } else if (indexPath.section == 3 || indexPath.section == 4) {
        return CGSizeMake(75, 30);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 2) {
        return UIEdgeInsetsMake(10, 31, 10, 31);
    } else if (section == 3 || section == 4) {
        return UIEdgeInsetsMake(10, 16, 10, 16);
    }
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section > 1) {
        return 20;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 2) {
        return (int)((SCREEN_WIDTH() - 262) / 4);
    } else if (section > 2) {
        if (SCREEN_WIDTH() == 320) {
            return 0;
        } else {        
            return (int)((SCREEN_WIDTH() - 32 - 75 * 4) / 3);
        }
    }
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        
        self.filterModel.carOutColorCode = self.model.baseCarOutColorOptions[indexPath.item].colorCode;
        self.filterModel.carOutColorName = self.model.baseCarOutColorOptions[indexPath.item].colorName;
        self.sectionHeaderModel[indexPath.section].detail = self.model.baseCarOutColorOptions[indexPath.item].colorName;
    } else if (indexPath.section == 3) {
        
        self.filterModel.carTypeId = self.model.baseCarTypeOptions[indexPath.item].carTypeId;
        self.filterModel.carTypeName = self.model.baseCarTypeOptions[indexPath.item].carTypeName;
        self.sectionHeaderModel[indexPath.section].detail = self.model.baseCarTypeOptions[indexPath.item].carTypeName;
    } else if (indexPath.section == 4) {
        self.filterModel.carEnvironName = self.model.baseCarEnvironOptions[indexPath.item];
        self.sectionHeaderModel[indexPath.section].detail = self.model.baseCarEnvironOptions[indexPath.item];
    }
    
    [UIView animateWithDuration:0 animations:^{
        [self->_mCollectionView performBatchUpdates:^{
            [self->_mCollectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
        } completion:nil];
    }];
}

#pragma mark - NRWSliderCollectionCellDelegate
- (void)sliderCollectionCell:(NRWSliderCollectionCell *)cell withFirstIndexPath:(int)firstIndexPath lastIndexPath:(int)lastIndexPath {
    
    NSUInteger section = [_mCollectionView indexPathForCell:cell].section;
    
    if (section == 0) {

        [NRWBuyCarFilterManager setValueForModel:self.filterModel headerModel:self.sectionHeaderModel.firstObject withTitleModel:self.carMileageTitleArray firstIndex:firstIndexPath lastIndex:lastIndexPath type:NRWBuyCarFilterTypeMileage];
    } else {
        
        [NRWBuyCarFilterManager setValueForModel:self.filterModel headerModel:self.sectionHeaderModel[1] withTitleModel:self.carAgeTitleArray firstIndex:firstIndexPath lastIndex:lastIndexPath type:NRWBuyCarFilterTypeCarAge];
    }
    
    [UIView animateWithDuration:0 animations:^{
        [self->_mCollectionView performBatchUpdates:^{
            [self->_mCollectionView reloadSections:[NSIndexSet indexSetWithIndex:section]];
        } completion:nil];
    }];
}

#pragma mark - getter
- (NRWGetFilterInfoRequest *)filterRequest {
    
    if (_filterRequest == nil) {
        _filterRequest = [[NRWGetFilterInfoRequest alloc] init];
        _filterRequest.delegate = self;
    }
    return _filterRequest;
}

- (NSArray<NRWBuyCarFilterHeaderModel *> *)sectionHeaderModel {
    
    if (_sectionHeaderModel == nil) {
        _sectionHeaderModel = [NRWBuyCarFilterManager getBuyCarFilterTitleArray];
    }
    return _sectionHeaderModel;
}

- (NSArray<NSString *> *)carMileageTitleArray {
    if (_carMileageTitleArray == nil) {
        _carMileageTitleArray = [NRWBuyCarFilterManager getBuyCarMileageTitleArray];
    }
    return _carMileageTitleArray;
}

- (NSArray<NSString *> *)carAgeTitleArray {
    if (_carAgeTitleArray == nil) {
        _carAgeTitleArray = [NRWBuyCarFilterManager getBuyCarAgeTitleArray];
    }
    return _carAgeTitleArray;
}

- (NRWBuyCarFilterModel *)filterModel {
    if (_filterModel == nil) {
        _filterModel = [[NRWBuyCarFilterModel alloc] init];
    }
    return _filterModel;
}

@end
